package com.socool.sinofaith.adapter;

import java.util.ArrayList;
import java.util.List;

import com.socool.sinofaith.R;
import com.socool.sinofaith.model.AuditTaskBean;
import com.socool.sinofaith.ui.AnswerQuestionActivity;
import com.socool.sinofaith.ui.SubTaskActivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class AuditRefusedAdapter extends BaseAdapter {

	private List<AuditTaskBean> mdatas = new ArrayList<AuditTaskBean>();
	private LayoutInflater inflater;

	private Context mContext;

	public List<AuditTaskBean> getMdatas() {
		return mdatas;
	}

	public void setMdatas(List<AuditTaskBean> mdatas) {
		this.mdatas = mdatas;
		this.notifyDataSetChanged();
	}

	public void addDatas(List<AuditTaskBean> mdatas) {
		this.mdatas.addAll(mdatas);
		this.notifyDataSetChanged();
	}

	public AuditRefusedAdapter(Context _context) {
		super();
		mContext = _context;
		inflater = LayoutInflater.from(_context);
	}

	@Override
	public int getCount() {
		if (mdatas != null) {
			return mdatas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {

		ViewHolder mViewHolder = null;
		if (convertView == null) {
			convertView = inflater
					.inflate(R.layout.auditrefused_listitem, null);
			mViewHolder = new ViewHolder();
			mViewHolder.templateName = (TextView) convertView
					.findViewById(R.id.templateNameTextView);
			mViewHolder.taskName = (TextView) convertView
					.findViewById(R.id.taskNameTextView);

			mViewHolder.btn_reanswer = (Button) convertView
					.findViewById(R.id.btn_reanswer);
			mViewHolder.isFlagAuditre = (TextView) convertView
					.findViewById(R.id.isFlag);
			convertView.setTag(mViewHolder);

		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		String TemplateName = "";
		if (!"".equals(mdatas.get(position).getTempNO())) {
			TemplateName = mdatas.get(position).getTempNO() + " - "
					+ mdatas.get(position).getTemplateName();
		} else {
			TemplateName = mdatas.get(position).getTemplateName();
		}
		
		
		if(!"".equals(mdatas.get(position).getTempNO())){
			String tempName="";
			if(mdatas.get(position).getTemplateName().contains("库房车辆")){
				tempName="库房车辆";
			}else if(mdatas.get(position).getTemplateName().contains("合格证")){
				tempName="合格证";
			}else{
				if(mdatas.get(position).getTemplateName().length()>4){
					
					tempName=mdatas.get(position).getTemplateName().substring(0, 3);
				}
				else{
					tempName=mdatas.get(position).getTemplateName();
				}
			}
			TemplateName=mdatas.get(position).getTempNO()+" - "+tempName;
		}else{
			TemplateName=mdatas.get(position).getTemplateName();
		}
		mViewHolder.templateName.setText(TemplateName);
		
		mViewHolder.templateName.setText(TemplateName);
		mViewHolder.taskName.setText(mdatas.get(position).getTaskName());
		String isAudit = "";
		if ("-1".equals(mdatas.get(position).getIsAudit())) {
			isAudit = "待审核";
		} else if ("0".equals(mdatas.get(position).getIsAudit())) {
			isAudit = "拒绝";
		} else if ("1".equals(mdatas.get(position).getIsAudit())) {
			isAudit = "通过";
		}
		if (mdatas.get(position).getFlag()!=null) {
			mViewHolder.isFlagAuditre.setVisibility(View.VISIBLE);
			mViewHolder.isFlagAuditre.setText(mdatas.get(position).getFlag());
		} else {
			mViewHolder.isFlagAuditre.setVisibility(View.INVISIBLE);
		}
		mViewHolder.btn_reanswer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext,
						AnswerQuestionActivity.class);
				intent.putExtra("type", "2");
				intent.putExtra("Answer_ID", mdatas.get(position)
						.getAnswer_ID());
				intent.putExtra("Template_ID", mdatas.get(position)
						.getTemplate_ID());
				intent.putExtra("taskid", mdatas.get(position).getTask_ID());
				intent.putExtra("subtaskid", mdatas.get(position)
						.getSubTask_ID());
				intent.putExtra("Stemp_ID", mdatas.get(position).getStemp_ID());
				intent.putExtra("taskName", mdatas.get(position)
						.getTemplateName());
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	public final class ViewHolder {
		public TextView templateName;
		public TextView taskName;
		TextView isFlagAuditre;
		public Button btn_reanswer;
	}

}
